/**
 * generator
 * 可以返回多次的函数
 */

function* numbers(){
    yield;
}

const gen=numbers();

console.log(gen.next());
console.log(gen.next());

//迭代器还原生成器的解构
function nameIterator(names){
    let nextIndex=0;
    return{
        next:function(){
            return nextIndex<names.length?
            {value:names[nextIndex++],done:false}:
            {value:undefined,done:true}
        }
    }
}

const nameArray=["Henry","Bucky","Amy"];
const names=nameIterator(nameArray);
// console.log(names.next());
// console.log(names.next());
// console.log(names.next());
// console.log(names.next());

function* sayNames(){
    yield 'Henry';
    yield 'Bucky';
    yield 'Amy';
}
const name=sayNames();

console.log(name.next());
console.log(name.next());
console.log(name.next());
console.log(name.next());

/**案例
 * id自增
 * es生成器
 */

 function* createIds(){
     let index=1;
     while(true){
         yield index++;
     }
 }

 const gen2=createIds();
 for(var i=0;i<10;i++){
     console.log(gen2.next().value);
 }